package com.qianbao.service.book;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qianbao.components.excepion.CommonException;
import com.qianbao.components.excepion.ExceptionEnum;
import com.qianbao.domain.book.BookCategory;
import com.qianbao.domain.book.BookEntity;
import com.qianbao.domain.book.BookListRequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BookService {


    /**
     * 获取书籍的分类
     * @param url
     * @return
     */
    public Map<String,List<BookCategory>> requestBookCategory(String url) throws CommonException{

        RestTemplate template = new RestTemplate();

        ResponseEntity<String> entity =
                template.getForEntity(url, String.class);


        JSONObject jsonObject = JSONObject.parseObject(entity.getBody());
        JSONArray jsonArray = jsonObject.getJSONArray("list");

        List<BookCategory> list = new ArrayList<>();
        for (Object object : jsonArray){
            JSONObject tempObject = (JSONObject) object;
            BookCategory bookCategory = new BookCategory();
            bookCategory.setCategoryId(tempObject.getString("id"));
            bookCategory.setCategoryName(tempObject.getString("category"));
            list.add(bookCategory);
        }

        if (list.size() <=0 ){
            throw new CommonException(ExceptionEnum.Book_category_Fail.getCode(),
                    ExceptionEnum.Book_category_Fail.getMessage());
        }

        Map<String,List<BookCategory>> dataMap = new HashMap();
        dataMap.put("list",list);

        return dataMap;
    }


    /**
     * 根据分类id获取所有列表数据
     * @param url
     * @param requestEntity
     * @return
     * @throws CommonException
     */
    public Map<String,List<BookEntity>> requestBookList(String url, BookListRequestEntity requestEntity) throws CommonException{

        // 请求
        RestTemplate template = new RestTemplate();

        // https://blog.csdn.net/ldy1016/article/details/80002126
        // Java RestTemplate post请求传递参数遇到的坑
        MultiValueMap<String,String> map = new LinkedMultiValueMap<>();
        map.add("category",requestEntity.getCategoryId());
        map.add("pageNo",requestEntity.getPageNo());
        map.add("pageSize",requestEntity.getPageSize());


        ResponseEntity<String> entity = template.postForEntity(url, map, String.class);

        // 转换
        JSONObject jsonObject = JSONObject.parseObject(entity.getBody());
        JSONArray jsonArray = jsonObject.getJSONArray("list");

        List<BookEntity> list = JSONArray.parseArray(jsonArray.toJSONString(), BookEntity.class);

        if (list.size() <=0 ){
            throw new CommonException(ExceptionEnum.Book_list_fail.getCode(),
                    ExceptionEnum.Book_list_fail.getMessage());
        }

        Map<String,List<BookEntity>> dataMap = new HashMap();
        dataMap.put("list",list);

        return dataMap;

    }





}
